<template>
  <el-dialog 
    title="选择设备型号" 
    v-model="visible" 
    width="800px" 
    append-to-body
    :before-close="handleClose">
    <el-form 
      :model="queryParams" 
      ref="queryRef" 
      :inline="true" 
      v-show="showSearch" 
      label-width="68px">
      <el-form-item label="型号名称" prop="modelName">
        <el-input 
          v-model="queryParams.modelName" 
          placeholder="请输入型号名称" 
          clearable 
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    
    <el-table 
      v-loading="loading" 
      :data="modelList" 
      @selection-change="handleSelectionChange" 
      @row-dblclick="handleRowDbClick" 
      ref="tableRef">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center" prop="id" />
      <el-table-column label="型号名称" align="center" prop="modelName" />
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="描述" align="center" prop="description" />
    </el-table>
    
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handlePageChange"
    />
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleConfirm">确 认</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { listDeviceModel } from "@/api/service/deviceModel" // 假设有这个API
import { handleSingleSelection } from "@/api/common"

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'select'])

// 定义响应式数据
const queryRef = ref(null)
const tableRef = ref(null)
const loading = ref(false)
const showSearch = ref(true)
const total = ref(0)

const visible = computed({
  get() {
    return props.modelValue
  },
  set(value) {
    emit('update:modelValue', value)
  }
})

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  modelName: undefined
})

const modelList = ref([])
const currentSelected = ref({})

// 监听visible变化，当对话框打开时加载数据
watch(visible, (newVal) => {
  if (newVal) {
    getList()
  } else {
    // 关闭时重置选择
    currentSelected.value = {}
  }
})

/** 查询设备型号列表 */
function getList() {
  loading.value = true
  // 这里需要替换为实际的API调用
  listDeviceModel(queryParams).then(response => {
    modelList.value = response.rows
    total.value = response.total
    loading.value = false
  }).catch(error => {
    console.error('获取设备型号列表失败:', error)
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  if (queryRef.value) {
    queryRef.value.resetFields()
  }
  handleQuery()
}

/** 分页操作 */
function handlePageChange(page) {
  queryParams.pageNum = page.page
  queryParams.pageSize = page.limit
  getList()
}

/** 处理选择变化 */
function handleSelectionChange(selection) {
  const result = handleSingleSelection(selection, tableRef.value)
  currentSelected.value = result.selectedItem || {}
}

/** 双击行 */
function handleRowDbClick(row) {
  currentSelected.value = row
  handleConfirm()
}

/** 确认选择 */
function handleConfirm() {
  if (Object.keys(currentSelected.value).length === 0) {
    // 显示提示信息
    alert('请先选择一条数据')
    return
  }
  
  emit('select', currentSelected.value)
  visible.value = false
}

/** 取消选择 */
function handleCancel() {
  visible.value = false
}

/** 关闭前处理 */
function handleClose() {
  visible.value = false
}
</script>

<style scoped>
/* 可以添加一些样式 */
</style>